﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using zjxj_repair.Bean;
using zjxj_repair.Service;
using zjxj_repair.Table;

namespace zjxj_repair.WindowsForm.Modal
{
    public partial class ConfigForm : Form
    {
        #region 成员实例
        EnableServices enableServices = new();
        #endregion

        public ConfigForm()
        {
            InitializeComponent();

            ConfigDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            ConfigDataGridView.DataSource = StaticBean.ConfigList;

            ConfigDataGridView.Columns["ToolName"].HeaderText = ConfigDataGridView.Columns["ToolName"].Name = "工具名称";
            ConfigDataGridView.Columns["ToolIp"].HeaderText = ConfigDataGridView.Columns["ToolIp"].Name = "工具IP";
            ConfigDataGridView.Columns["ToolPort"].HeaderText = ConfigDataGridView.Columns["ToolPort"].Name = "工具端口";

            ConfigDataGridView.Columns["工具名称"].ReadOnly = true;

            ConfigDataGridView.Columns["Id"].Visible = false;
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                List<RepairToolConfig> repairs = ConfigDataGridView.DataSource as List<RepairToolConfig>;

                if (repairs == null)
                {
                    MessageBox.Show("数据不可为空！");

                    return;
                }

                enableServices.UpdateToolConfig(repairs);

                StaticBean.ConfigList = repairs;

                MessageBox.Show("更新成功！");

                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("更新失败，原因是" + ex.Message);
            }
        }
    }
}
